package cn.jiyun.controller;

import cn.jiyun.common.GeocodingService;
import cn.jiyun.common.config.Result;
import cn.jiyun.entity.AreaVo;
import cn.jiyun.entity.GeoResult;
import cn.jiyun.entity.ZhylArea;
import cn.jiyun.service.SbglService;
import cn.jiyun.service.ZhylAreaService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping("/zhylArea")
@CrossOrigin
public class ZhylAreaController {
    @Autowired
    private ZhylAreaService zhylAreaService;
    @Autowired
    private GeocodingService geocodingService;
    @Autowired
    private SbglService sbglService;
    @RequestMapping("findPage")
    public Result findPage(Integer pageNum, Integer pageSize, @RequestBody AreaVo areaVo) {
        //查询分页
        Page<ZhylArea> page = zhylAreaService.findPage(pageNum, pageSize, areaVo);
        return Result.ok(page);
    }

    @RequestMapping("findById")
    public Result findId(Integer id) {
        ZhylArea zhylArea = zhylAreaService.getById(id);
        return Result.ok(zhylArea);
    }

    @RequestMapping("update")
    public Result update(@RequestBody ZhylArea zhylArea) {
        zhylArea.setUpdatedAt(new Date());
        boolean b = zhylAreaService.updateById(zhylArea);
        return Result.ok(b);
    }
    @RequestMapping("toadd")
    public Result toadd(@RequestBody ZhylArea zhylarea){
        zhylarea.setStatus(1);
        zhylarea.setCreatedAt(new Date());
        zhylarea.setCoordinateType("高德经纬度");
        GeoResult geocode = geocodingService.getGeocode(zhylarea.getAddress());
        String[] split = geocode.getLocation().split(",");
        zhylarea.setLongitude(new BigDecimal(split[0]));
        zhylarea.setLatitude(new BigDecimal(split[1]));
        zhylAreaService.save(zhylarea);
        return Result.ok();
    }
    @RequestMapping("findSbgl")
    public Result findSbgl(){
        return Result.ok(sbglService.list(null));
    }
}
